package com.ycsj.goldmedalnewconcept.student.activity;


import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.just.agentweb.AgentWeb;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的收藏
 * Created by Administrator on 2019/5/8 0008.
 */

public class MyCollectionListActivity extends BaseActivity {

    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.webview)
    WebView mWebview;

    private AgentWeb mAgentWeb;
    private WebSettings mWebSettings;

    //收藏的URL
    private String COLLECTIONURL = HttpAddress.COLLECTIONQUESTION;

    /**
     * 跳转到我的收藏的界面
     *
     * @param context
     */
    public static void goActivity(Context context) {
        Intent it = new Intent(context, MyCollectionListActivity.class);
        context.startActivity(it);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_actiivty_mycollection;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化标题
        initTitle();

        //初始化配置
        initAndAddWebView();

        //初始化WebView
        intiWebView();

    }

    /**
     * 设置WebView
     */
    private void intiWebView() {

        mWebSettings = mWebview.getSettings();

        // 设置与Js交互的权限
        mWebSettings.setJavaScriptEnabled(true);

        // 设置允许JS弹窗
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        mWebview.loadUrl(COLLECTIONURL);

        mWebview.addJavascriptInterface(new WebAppInterface(this), "Android");


        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("我的收藏");
    }

    /**
     * 设置AgentWeb
     */
    private void initAndAddWebView() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(rlContent, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(COLLECTIONURL);

        mAgentWeb.getJsInterfaceHolder().addJavaObject("Android", new AndroidInterface(mAgentWeb, this));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }

        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) {
            mWebview.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();

        if (mWebview != null) {
            mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebview.clearHistory();

            ((ViewGroup) mWebview.getParent()).removeView(mWebview);
            mWebview.destroy();
            mWebview = null;
        }
        super.onDestroy();
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick(R.id.iv_back)
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }

    /**
     * Js和Android的回调接口
     */
    public class AndroidInterface {

        private AgentWeb agent;

        private Context context;

        public AndroidInterface(AgentWeb agent, Context context) {
            this.agent = agent;
            this.context = context;
        }

        @JavascriptInterface
        public String getParam() {
//        String param = AccountUtils.getAccount(context)+","+ AccountUtils.getRole(context);
            String param = "13333330031" + "," + "0";
            return param;
        }
    }


    /**
     * JS和Android的回调接口
     */
    public class WebAppInterface {
        Context mContext;

        WebAppInterface(Context c) {
            mContext = c;
        }

        // 传递参数
        @JavascriptInterface
        public String getParam() {
//            String param = "13006314760" + ",0";
            String param = AccountUtils.getAccount(mContext) + "," + AccountUtils.getRole(mContext);
            return param;
        }
    }


}
